<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= ""; /*  . htmlentities($_SERVER['QUERY_STRING']) */
}
if(isset($_POST['MM_insert']))
	{
	if(isset($_POST['Code_offre']) && ($_POST['Code_offre'])!=NULL )
		{
			if(veriflibelle1("Code_offre"))
			{
				$nums=existeChamp($database_microassur, $microassur,"offre","Code_offre", $_POST['Code_offre']);
					if($nums>=intval(1))
						{
						$message = colorer("Cette offre existe déjà","false"); 
						}
					else
						{
							$veriflibelle=verifLibelle("Libelle_offre");
								if($veriflibelle==TRUE)
								{
									if(isset($_POST['Date_debut_offre']) && $_POST['Date_debut_offre']!=NULL)
									{
											$datedebut = datevalider($_POST['Date_debut_offre']);
											$datevalide1 = datevalide($_POST['Date_debut_offre']);
											if(isset($_POST['Date_fin_offre']) && $_POST['Date_fin_offre']!=NULL){ $datefin = datevalider($_POST['Date_fin_offre']); $datevalide2 = datevalide($_POST['Date_fin_offre']); }
											if($datedebut && $datevalide1)
											{
												$lesdates=verifDate($_POST['Date_debut_offre'],$_POST['Date_fin_offre']);
												if($lesdates==TRUE)
												{
														if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
														{
														  $insertSQL = sprintf("INSERT INTO offre (Identifiant_offre, Code_offre, Libelle_offre, Date_debut_offre, Date_fin_offre) VALUES (%s, %s, %s, %s, %s)",
																			   GetSQLValueString($_POST['Identifiant_offre'], "int"),
																			   GetSQLValueString($_POST['Code_offre'], "text"),
																			   GetSQLValueString($_POST['Libelle_offre'], "text"),
																			   GetSQLValueString(dateEN2FR($_POST['Date_debut_offre']), "date"),
																			   GetSQLValueString(dateEN2FR($_POST['Date_fin_offre']), "date"));
																			   
															mysql_select_db($database_microassur, $microassur);
															  mysql_query("START TRANSACTION");
															$Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
															if($Result1==intval(1))
															{	mysql_query("commit");
																$message = colorer("L'offre est créée","true");
																unset($_POST);
															}
															else {
																mysql_query("rollback");
																$message = colorer("Erreur: Contacter la DSI ","false");
																}
														}
												}
												else
													{
														if(!$datefin)
															$message = colorer("Le format de la date de fin de commercialisation est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false");
														elseif(!$datevalide2)
															$message = colorer("La date de fin de commercialisation est incorrecte ","false");
														elseif(!$lesdates)
															$message = colorer("La date de fin de commercialisation doit être postérieure à la date de début de commercialisation","false");
													}
											}
											else
												{
													if(!$datedebut)
														$message = colorer("Le format de la date de début de commercialisation est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false");
													elseif(!$datevalide1)
														$message = colorer("La date de début de commercialisation est incorrecte ","false");
												}
									}
									else
										$message = colorer("Vous devez saisir la date de début de commercialisation","false");
								}
								else
									$message = colorer("Vous devez saisir le libellé","false");
						}
			}
			else
				$message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
		}
		else
			$message = colorer("Vous devez saisir le code","false");
	}
$maxRows_rs_liste_offre = 10;
$pageNum_rs_liste_offre = 0;
if (isset($_GET['pageNum_rs_liste_offre'])) {
  $pageNum_rs_liste_offre = $_GET['pageNum_rs_liste_offre'];
}
$startRow_rs_liste_offre = $pageNum_rs_liste_offre * $maxRows_rs_liste_offre;

mysql_select_db($database_microassur, $microassur);
$query_rs_liste_offre = "SELECT o.* FROM offre as o";
//$query_limit_rs_liste_offre = sprintf("%s LIMIT %d, %d", $query_rs_liste_offre, $startRow_rs_liste_offre, $maxRows_rs_liste_offre);
$query_limit_rs_liste_offre = $query_rs_liste_offre;
$rs_liste_offre = mysql_query($query_limit_rs_liste_offre, $microassur) or die(mysql_error());
$row_rs_liste_offre = mysql_fetch_assoc($rs_liste_offre);

if (isset($_GET['totalRows_rs_liste_offre'])) {
  $totalRows_rs_liste_offre = $_GET['totalRows_rs_liste_offre'];
} else {
  $all_rs_liste_offre = mysql_query($query_rs_liste_offre);
  $totalRows_rs_liste_offre = mysql_num_rows($all_rs_liste_offre);
}
$totalPages_rs_liste_offre = ceil($totalRows_rs_liste_offre/$maxRows_rs_liste_offre)-1;

$queryString_rs_liste_offre = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rs_liste_offre") == false && 
        stristr($param, "totalRows_rs_liste_offre") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rs_liste_offre = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rs_liste_offre = sprintf("&totalRows_rs_liste_offre=%d%s", $totalRows_rs_liste_offre, $queryString_rs_liste_offre);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../offre.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Offre";
					$nommodule2="Liste des offres";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
				<table class="tab_list">
						  <tr id="niv1">
							<td class="cod_ofr">CODE</td>
							<td class="lib_ofr">LIBELLE</td>
							<td class="date_ofr">DATE DEBUT ET FIN</td>
						 </tr>
                 </table>
				<div class="content_table">
					<table class="tab_list ns0">
						 
						<?php $i=1; do 	{
						?>
						<?php if ($totalRows_rs_liste_offre > 0) { // Show if recordset not empty ?>
								<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
								  <td class="cod_ofr"><a href="modoffre.php?id=<?php echo $row_rs_liste_offre['Identifiant_offre']; ?>"><?php echo $row_rs_liste_offre['Code_offre']; ?></a></td>
								  <td class="lib_ofr"><a href="modoffre.php?id=<?php echo $row_rs_liste_offre['Identifiant_offre']; ?>"><?php echo $row_rs_liste_offre['Libelle_offre']; ?></a></td>
								  <td class="date_ofr"><a href="modoffre.php?id=<?php echo $row_rs_liste_offre['Identifiant_offre']; ?>"><?php echo dateEN2FR($row_rs_liste_offre['Date_debut_offre']); ?> / <?php if ($row_rs_liste_offre['Date_fin_offre']!=NULL) echo dateEN2FR($row_rs_liste_offre['Date_fin_offre']); else echo $indefini; ?></a></td>
								</tr>
						<?php
						}
							$i++;
									} while ($row_rs_liste_offre = mysql_fetch_assoc($rs_liste_offre));
						?>
					</table>
				</div>
			</div>
			<div class="imgright"></div>
		<div class="cleared"></div>
		<!--/***************************************/-->
		<div id="br"></div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
					<div class="content_tableform">
							  <table>
								<tr>
								  <td>CODE</td>
								  <td><input class="resize_1" type="text" name="Code_offre" value="<?php echo remind_text("Code_offre"); ?>" size="15" maxlength="1" /></td>
								</tr>
								<tr>
								  <td>LIBELLE</td>
								  <td><input type="text" name="Libelle_offre" value="<?php echo remind_text("Libelle_offre"); ?>" size="50" maxlength="30" /></td>
								</tr>
								<tr>
								  <td>DATE DEBUT COMMERCIALISATION</td>
								  <td><input class="resize_2 date" type="text" name="Date_debut_offre" value="<?php echo retourValForm("Date_debut_offre"); ?>" size="20" maxlength="10" /> </td>
								</tr>
								<tr>
								  <td>DATE FIN COMMERCIALISATION</td>
								  <td><input class="resize_2 date" type="text" name="Date_fin_offre" value="<?php echo retourValForm("Date_fin_offre"); ?>" size="20" maxlength="10" /> </td>
								</tr>
							  </table>
					</div>
			</div>
			<div class="imgright"></div>				
				<div class="cleared"></div>
				
					<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
					<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
					
				<div class="cleared"></div>
		<input type="hidden" name="Identifiant_offre" value="" />
		<input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs_liste_offre);
?>